package bsts.controller;

import java.text.SimpleDateFormat;
import java.util.*;

import bsts.model.Plan;
import bsts.model.User;
import bsts.model.Account;
import bsts.model.Subscription;

public class SubscriptionController {

	private IOController ioController;
	private static long SUBSCRIPTIONID = 201100000000L;
	private HashMap<Long, Account> SupscriptionToAccountMap = new HashMap<Long, Account>();
	private HashMap<Long, Subscription> SupscriptionMap = new HashMap<Long, Subscription>();
	private ArrayList<Plan> defaultPlans = new ArrayList<Plan>();

	public SubscriptionController() {
		ioController = new IOController();
		defaultPlans = ioController.RetrievePlans();
		SUBSCRIPTIONID = ioController.RetrieveSubscriptionID();
	}

	private long generateSubId() {
		SimpleDateFormat sdfYear = new SimpleDateFormat("yyyyMMdd");

		String subNo = sdfYear.format(new Date());
		SUBSCRIPTIONID++;
		ioController.UpdateSubscriptionID(SUBSCRIPTIONID);
		if (SUBSCRIPTIONID < 10) {
			return Long.parseLong(subNo + "000" + SUBSCRIPTIONID);
		} else if (SUBSCRIPTIONID < 100) {
			return Long.parseLong(subNo + "00" + SUBSCRIPTIONID);
		} else if (SUBSCRIPTIONID < 1000) {
			return Long.parseLong(subNo + "0" + SUBSCRIPTIONID);
		} else {
			return Long.parseLong(subNo + SUBSCRIPTIONID);
		}
	}

	public ArrayList<Plan> getDefaultPlans() {
		return defaultPlans;
	}
	
	public Plan getDefaultPlan(long id) {
		for (Plan plan : defaultPlans) {
			if (plan.getPlanId() == id)
				return plan;
		}
		
		return null;
	}

	public Subscription createSubstription(Account account, Date dateCommenced,
			Date dateTerminated, Plan plan, User user) {
		Subscription subscription = new Subscription(generateSubId(),
				dateCommenced, dateTerminated, plan, user);
		SupscriptionToAccountMap.put(subscription.getSubscriptionId(), account);
		SupscriptionMap.put(subscription.getSubscriptionId(), subscription);

		return subscription;
	}

	public boolean deleteSubscription(long subscriptionId) {
		if (SupscriptionToAccountMap.remove(subscriptionId) != null
				&& SupscriptionMap.remove(subscriptionId) != null)
			return true;
		else
			return false;
	}

	public Subscription retrieveSubscription(long subscriptionId) {
		if (SupscriptionToAccountMap.containsKey(subscriptionId)
				&& SupscriptionMap.containsKey(subscriptionId))
			return SupscriptionMap.get(subscriptionId);
		else
			return null;

	}

	// Method defination created by Than Htet Aung : 21/March/2011
	public ArrayList<Subscription> retrieveSubscription(String accountNo) {
		return new ArrayList<Subscription>();
	}

	public boolean updateSubscription(long subscriptionId, Account account,
			Subscription subscription) {
		if (retrieveSubscription(subscriptionId) != null) {
			SupscriptionToAccountMap.put(subscriptionId, account);
			SupscriptionMap.put(subscriptionId, subscription);
			return true;
		} else
			return false;
	}

	public boolean updatePlan(long subscriptionId, Plan plan) {
		if (retrieveSubscription(subscriptionId) != null) {
			Subscription tempSubscription = SupscriptionMap.get(subscriptionId);
			tempSubscription.setPlan(plan);
			SupscriptionMap.put(subscriptionId, tempSubscription);
			return true;
		} else
			return false;

	}

}
